<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $sc_user = $this->session->userdata('co_usuario');
        $sc_lvl = $this->session->userdata('co_tipo_usuario');
        if ($sc_user) {
            if ($sc_lvl === '2') {
                $this->index_crd();
            } else {
                $this->index_aluno();
            }
        } else {
            $rt = urlencode($this->uri->uri_string());
            redirect('/?rtn=' . $rt, 'refresh');
        }
    }

    public function index_aluno() {
        $this->load->helper('dashboard');
        $this->load->model('Model_home');
        $user = $this->session->userdata('co_usuario');
        $dataAlu = ['aluno' => $this->Model_home->dadosAluno($user)];
        $arquivosJS = array('jquery', 'jquery-ui', 'canvasjs.min', 'dashboard', 'main', 'countTo');
        $arquivosCSS = array('main', 'dashboard');
        $params = array("arquivosJS" => $arquivosJS,
            "arquivosCSS" => $arquivosCSS,
            "titulo" => "SONIC :: Página Inicial");
        $this->load->view('header', $params);
        $this->load->view('aluno/menu_lateral');
        $this->load->view('aluno/home', $dataAlu);
        $this->load->view('footer');
    }

    public function index_crd() {
        $arquivosJS = array('jquery', 'jquery-ui', 'canvasjs.min', 'dashboard', 'main_crd', 'countTo');
        $arquivosCSS = array('main', 'dashboard_crd');
        $params = array("arquivosJS" => $arquivosJS,
            "arquivosCSS" => $arquivosCSS,
            "titulo" => "SONIC :: Página Inicial");
        $this->load->view('header', $params);
        $this->load->view('crd/menu_lateral');
        $this->load->view('footer');
    }

    public function sessiondata() {
        ajax_only();
        $acaoSS = $this->input->get('acao');
        switch ($acaoSS) {
            case "verificar":
                $UserCtx = $this->session->userdata('contexto');
                echo (empty($UserCtx)) ? 0 : 1;
                break;
            case "listarcontexto":
                $this->load->model('Model_home');
                $user = $this->session->userdata('co_usuario');
                $ctx_ls = $this->Model_home->listar_ctx($user);
                $ano = date('Y');
                $sm_atual = (date('m') <= 6 ) ? 1 : 2;
                $sm_anterior = ($sm_atual - 1 === 0) ? ($ano - 1) . '/2' : $ano . '/1';
                $sm_posterior = ($sm_atual + 1 === 3) ? ($ano + 1) . '/1' : $ano . '/2';
                $sm = ['ls' => ['anterior' => $sm_anterior, 'atual' => $ano . '/' . $sm_atual, 'proximo' => $sm_posterior],
                    'data_limite' => ($sm_atual == 1) ? $ano . '-06-01' : $ano . '-12-01'];
                if ($sm['data_limite'] > date('Y-m-d')) {
                    array_pop($sm['ls']);
                }
                $lsContexto = '<h1>Escolha o contexto educacional</h1> <select name="contexto" id="contexto">' .
                        '<option value="">Selecione o contexto educacional</option>';
                foreach ($sm['ls'] as $ctx) {
                    $dsb = (!in_array($ctx, $ctx_ls) && $ctx === $sm['ls']['anterior']) ? ' disabled' : '';
                    $new_sm = (!in_array($ctx, $ctx_ls)) ? '|new' : '';
                    $lsContexto .= '<option value="' . $ctx . $new_sm . '"' . $dsb . '>' . $ctx . '</option>';
                }
                $lsContexto .= '</select><p>&nbsp;</p><button id="confs" class="botao">Salvar</button>';
                echo $lsContexto;
                break;
            case "setar" :
                $txt = $this->input->get('texto');
                $val = $this->input->get('valor');
                $ctxGet = $this->input->get('ctx');
                if (!empty($ctxGet)) {
                    $user = $this->session->userdata('co_usuario');
                    $ctxBd = str_replace('/', '', $txt);
                    $ctx_data = ['CO_USUARIO' => $user, 'NU_SEMESTRE_LETIVO' => $ctxBd];
                    $this->load->model('Model_home');
                    $this->Model_home->criar_ctx($ctx_data);
                }
                $this->session->set_userdata('contexto', $txt);
                echo $txt;
                break;
            case "exibir" :
                echo $this->session->userdata('contexto');
                break;
            case "removercontexto":
                $this->session->unset_userdata('contexto');
                echo 'O contexto foi removido';
                break;
            default:
                echo 'Especifique uma ação!';
                break;
        }
    }

    public function notificacao() {
        ajax_only();
        header('Content-Type: application/json');
        $this->load->model('Model_home');
        $usuario = $this->session->userdata('co_usuario');
        $tpUser = $this->session->userdata('co_tipo_usuario');
        if ($tpUser === '3') {
            $contexto = str_replace('/', '', $this->session->userdata('contexto'));
            $jsonNoty = $this->Model_home->notificacao($usuario, $contexto);
        } else {
            $jsonNoty = $this->Model_home->ntf_crd($usuario);
        }
        $notJSON = json_encode($jsonNoty);
        echo $notJSON;
    }

    public function rqrOpenUser() {
        $this->load->model('Model_home');
        $usuario = $this->session->userdata('co_usuario');
        $openRQR = $this->Model_home->rqrOpen($usuario);
        return $openRQR;
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */